package com.alipay.app.groupmeal.standard.bean;

import com.alipay.app.groupmeal.standard.offline.bean.FkBean;
import com.alipay.app.groupmeal.standard.util.ByteUtil;
import com.alipay.app.groupmeal.standard.util.HexDump;

import java.math.BigDecimal;
import java.math.BigInteger;

public class SerialPortSend {

    private String head; //包头、起始符

    private String packetLength; //包长度

    private String requestCode; //请求码, 01查询余额; 02请求支付; 03交易未决

    private String returnCode; //返回码, 00成功; 01失败

    private String packetSerialNumber; //包序号;00~FF

    private String retainArea; //保留区域

    private String cardSn; //卡序列号

    private String costAmount; //消费金额

    private String kyeBalance; //扣款后余额1

    private String btYeBalance; //扣款后余额2

    private String verifyCode; //校验位

    private String foot; //包尾、结束符

    private static SerialPortSend instance;

    public static synchronized SerialPortSend getInstance() {
        if (instance == null) {
            instance = new SerialPortSend();
            instance.setHead("5A3C");
            instance.setFoot("DB");
            instance.setPacketLength("1A");
            instance.setReturnCode("00");
            instance.setRetainArea();
        }
        return instance;
    }

    public void sendData(FkBean fk) {
        String amount = SerialPortReceive.getInstance().getCostAmount();
        int divideCost = new BigDecimal(amount).multiply(new BigDecimal(100)).intValue();
        setCostAmount(divideCost);
        int divideKye = BigDecimal.valueOf(fk.getKye()).multiply(new BigDecimal(100)).intValue();
        setKyeBalance(divideKye);
        int divideBtYe = BigDecimal.valueOf(fk.getBtye()).multiply(new BigDecimal(100)).intValue();
        setBtYeBalance(divideBtYe);
        setCardSn(fk.getCardsn().trim());
        setRequestCode(SerialPortReceive.getInstance().getRequestCode());
        setPacketSerialNumber(SerialPortReceive.getInstance().getPacketSerialNumber());
        setVerifyCode();
    }

    public String getHead() {
        return head;
    }

    public void setHead(String head) {
        this.head = head;
    }

    public String getPacketLength() {
        return packetLength;
    }

    public void setPacketLength(String packetLength) {
        this.packetLength = packetLength;
    }

    public String getRequestCode() {
        return requestCode;
    }

    public void setRequestCode(String requestCode) {
        this.requestCode = requestCode;
    }

    public String getReturnCode() {
        return returnCode;
    }

    public void setReturnCode(String returnCode) {
        this.returnCode = returnCode;
    }

    public String getPacketSerialNumber() {
        return packetSerialNumber;
    }


    public void setPacketSerialNumber(String packetSerialNumber) {
        this.packetSerialNumber = packetSerialNumber;
    }

    public String getRetainArea() {
        return retainArea;
    }

    private void setRetainArea() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 7; i++) {
            sb.append("00");
        }
        this.retainArea = sb.toString();
    }

    public String getCardSn() {
        return cardSn;
    }

    public void setCardSn(String cardSn) {
        byte[] temp = ByteUtil.hexStringToByte(cardSn);
        byte[] byteArray = new byte[temp.length];
        for (int i = 0; i < temp.length; i++) {
            byteArray[i] = temp[temp.length - 1 - i];
        }
        this.cardSn = HexDump.toHexString(byteArray);;
    }

    public String getCostAmount() {
        return costAmount;
    }

    public void setCostAmount(int costAmount) {
        String hexString = Integer.toHexString(costAmount);
        BigInteger bigInteger = new BigInteger(hexString, 16);
        byte[] temp = bigInteger.toByteArray();
        byte[] byteArray = new byte[temp.length];
        for (int i = 0; i < temp.length; i++) {
            byteArray[i] = temp[temp.length - 1 - i];
        }
        String result = HexDump.toHexString(byteArray);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 8 - result.length(); i++) {
            sb.append("0");
        }
        this.costAmount = result + sb;
    }

    public String getKyeBalance() {
        return kyeBalance;
    }

    private void setKyeBalance(int divideKye) {
        String hexString = Integer.toHexString(divideKye);
        BigInteger bigInteger = new BigInteger(hexString, 16);
        byte[] temp = bigInteger.toByteArray();
        byte[] byteArray = new byte[temp.length];
        for (int i = 0; i < temp.length; i++) {
            byteArray[i] = temp[temp.length - 1 - i];
        }
        String result = HexDump.toHexString(byteArray);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 8 - result.length(); i++) {
            sb.append("0");
        }
        this.kyeBalance = result + sb;
    }

    public String getBtYeBalance() {
        return btYeBalance;
    }

    private void setBtYeBalance(int divideBtYe) {
        String hexString = Integer.toHexString(divideBtYe);
        BigInteger bigInteger = new BigInteger(hexString, 16);
        byte[] temp = bigInteger.toByteArray();
        byte[] byteArray = new byte[temp.length];
        for (int i = 0; i < temp.length; i++) {
            byteArray[i] = temp[temp.length - 1 - i];
        }
        String result = HexDump.toHexString(byteArray);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 8 - result.length(); i++) {
            sb.append("0");
        }
        this.btYeBalance = result + sb;
    }

    public String getVerifyCode() {
        return verifyCode;
    }

    public void setVerifyCode() {
        String verifyString = this.requestCode + this.returnCode + this.packetSerialNumber
                + this.retainArea + this.cardSn + this.costAmount + this.kyeBalance + this.btYeBalance;
        int sum = 0;
        for (int i = 0; i < verifyString.length() / 2; i++) {
            String substring = verifyString.substring(i * 2, i * 2 + 2);
            sum += Integer.parseInt(substring, 16);
        }
        String hexString = Integer.toHexString(sum).toUpperCase();
        this.verifyCode = hexString.substring(hexString.length() - 2);
    }

    public String getFoot() {
        return foot;
    }

    public void setFoot(String foot) {
        this.foot = foot;
    }

    public String getSendCommand() {
        return this.head +
                this.packetLength +
                this.requestCode +
                this.returnCode +
                this.packetSerialNumber +
                this.retainArea +
                this.cardSn +
                this.costAmount +
                this.kyeBalance +
                this.btYeBalance +
                this.verifyCode +
                this.foot;
    }
}
